#!/bin/sh
#
# This file is an abomination. It executes as both a shell script and a python
# script, and does the same thing in both cases.
#
# Note that this script uses bash syntax but the shebang is `#!/bin/sh` on
# purpose. This is because the bash code only runs on MSYS and `/bin/sh` is
# bash there. macOS will stop shipping bash at some point (post-Catalina), so
# we should not use `#!/bin/bash` as the shebang. Current code is compatible
# on non-MSYS platforms with: bash, dash, zsh.
#
# This is needed because on Windows, python3 is actually `py -3` since all
# Python installations have `python.exe`. We used to use `python3` in the
# shebang, but then Windows folks couldn't use `./cerbero-uninstalled` and had
# to call `py -3 ./cerbero-uninstalled` or `python ./cerbero-uninstalled`
# depending on their python installation.
#
# Now we automatically use `py -3` or `python3` depending on the system.
#
# For backwards-compatibility, we now support all those variants.

"""":

ARGS=$@
SCRIPTDIR="`dirname $0`"

case "$MSYSTEM" in
    *MINGW*) PYTHON="py -3";;
    *) PYTHON="python3";;
esac

# Use `msysmnt` to get a list of MSYS mount points that the MinGW shell uses.
# That's our reference point for translating from MSYS paths to Win32 paths.
# We assume that the MSYS mount point directories are only in the filesystem
# root. This will break if people add their own custom mount points beyond what
# MSYS automatically creates, which is highly unlikely.
#
# /d -> d:/
# /c -> c:/
# /d/projects/cerbero -> d:/projects/cerbero/
# /home/USERNAME/cerbero -> C:\\MinGW\\msys\\1.0/home/USERNAME/
# /mingw -> C:\\MinGW/
# /mingw/bin/foobar -> C:\\MinGW\\bin/foobar/
# /tmp/baz -> C:\\Users\\USERNAME\\AppData\\Local\\Temp/baz/
msys_dir_to_win32() {
    set -e
    local msys_path stripped_path mount_point path mounted_path
    # If the path is already a native path, just return that
    if [[ $1 == ?':/'* ]] || [[ $1 == ?':\\'* ]]; then
      echo $1
      return
    fi
    # Convert /c or /mingw etc to /c/ or /mingw/ etc; gives us a necessary
    # anchor to split the path into components
    msys_path="$1/"
    # Strip leading slash
    stripped_path="${msys_path#/}"
    # Get the first path component, which may be a mount point
    mount_point="/${stripped_path%%/*}"
    # Get the path inside the mountp oint
    path="/${stripped_path#*/}"
    mounted_path="$(msysmnt | sed -n "s|\(.*\) on $mount_point type.*|\1|p")"
    # If it's not a mounted path (like /c or /tmp or /mingw), then it's in the
    # general MSYS root mount
    if [[ -z $mounted_path ]]; then
        mounted_path="$(msysmnt | sed -n "s|\(.*\) on / type.*|\1|p")"
        path="$1"
    fi
    echo ${mounted_path}${path}
}

get_scriptdir() {
    if [ -n "$MSYSTEM" ]; then
        # Get the win32 path, instead of the Cygwin one
        msys_dir_to_win32 "$SCRIPTDIR"
    else
        echo $SCRIPTDIR
    fi
}

$PYTHON -c """
import os
import sys
import shlex

os.environ['CERBERO_UNINSTALLED'] = '1'

# __file__ is not set when we're called with -c
if '__file__' in locals():
    curdir = os.path.dirname(__file__)
else:
    curdir = '`get_scriptdir`'
sys.path.insert(0, os.path.abspath(curdir))

# Need to set sys.argv since we can't pass args when called with -c
if sys.argv == ['-c']:
    sys.argv = ['$0'] + shlex.split('$ARGS')

from cerbero.main import main
main()
#"""
